﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CubicFWNet.Application.SystemManager;
using CubicFWNet.Common;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.ViewModal;
using PagedList;

namespace CubicFWNet.Mvc.Controllers
{
    public class FinanceCostController : ControllerBase
    {
        private FinanceCostApp _financeCostApp;
        private OrderProductApp _orderProductApp;
        private OperateRecordApp _operateRecordApp;
        private ProfitRatioApp _profitRatioApp;
        private InvoiceApp _invoiceApp;
        private OrderApp _orderApp;
        private CustomerApp _customerApp;
        private TechnologyApp _technologyApp;
        private OriginalCostApp _originalCostApp;
        private FinanceInvoiceApp _financeInvoiceApp;
        private ArapApp _arapApp;
        private PurchaseApp _purchaseApp;
        public FinanceCostController()
        {
            _financeCostApp = new FinanceCostApp();
            _orderProductApp = new OrderProductApp();
            _operateRecordApp = new OperateRecordApp();
            _profitRatioApp = new ProfitRatioApp();
            _invoiceApp = new InvoiceApp();
            _orderApp = new OrderApp();
            _customerApp = new CustomerApp();
            _technologyApp = new TechnologyApp();
            _originalCostApp = new OriginalCostApp();
            _financeInvoiceApp = new FinanceInvoiceApp();
            _arapApp = new ArapApp();
            _purchaseApp = new PurchaseApp();
        }

        // GET: FinanceCost
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult CheckOrder(int page = 1, string productName = "", string number = "", string beginTime = "", string endTime = "")
        {
            ViewBag.productName = productName.Trim();
            ViewBag.number = number.Trim();
            DateTime _beginTime = DateTime.Parse("2000-01-01 10:10:10");
            DateTime _endTime = DateTime.Now.Date.AddDays(1);
            ViewBag.beginTime = "";
            ViewBag.endTime = "";
            if (!string.IsNullOrEmpty(beginTime) && Validate.IsDate(beginTime))
            {
                _beginTime = DateTime.Parse(beginTime.Trim());
                ViewBag.beginTime = beginTime.Trim();
            }
            if (!string.IsNullOrEmpty(endTime) && Validate.IsDate(endTime))
            {
                _endTime = DateTime.Parse(endTime.Trim());
                ViewBag.endTime = endTime.Trim();
            }

            IPagedList<ProductTechnologyView> tecjList = _financeCostApp.GetList(4, -1, productName.Trim(), number.Trim(), _beginTime, _endTime).ToPagedList(page, 10);
            return View(tecjList);
        }

        public ActionResult CheckInfo(Guid opId)
        {
            ProductTechnologyView ptv = _orderProductApp.GetOrderProductsByOrderProductId(opId);
            FinanceCost financeCost = _financeCostApp.GetSingleFinanceCostByOrderProductId(opId);
            //ViewBag.ptlist = list;
            Technology t = _technologyApp.GetSingleTechnology(ptv.TechnologyId);
            Equipment equipment = _technologyApp.GetSingleEquipment(t.EquipmentType);
            GradeMaintenance gmTech = _technologyApp.GetSingleGradeMaintenance(t.TechType, 1);//检验
            GradeMaintenance gmWork = _technologyApp.GetSingleGradeMaintenance(t.TechType, 2);//加工
            TechnologyManageView tmweb = _technologyApp.GetTechnologyManageSingle(ptv.ProductId);

            OriginalCost oc = _originalCostApp.GetSingleOriginalCostByTid(t.Id);
            decimal zhuMoney = 0;
            decimal fuMoney = 0;
            if (oc != null && !string.IsNullOrEmpty(oc.Data))
            {
                List<JasonData> jdList = Common.Json.ToObject<List<JasonData>>(oc.Data);
                var zhuObj = jdList.FirstOrDefault(a => a.des == tmweb.MaterialId.ToString());
                var fuObjList = jdList.Where(a => a.des != tmweb.MaterialId.ToString()).ToList();
                if (zhuObj != null && Validate.IsDecimalByParse(zhuObj.value))
                {
                    zhuMoney = Convert.ToDecimal(zhuObj.value);
                }
                foreach (var fuObj in fuObjList)
                {
                    if (fuObj != null && Validate.IsDecimalByParse(fuObj.value))
                    {
                        fuMoney += Convert.ToDecimal(fuObj.value);
                    }
                }
            }
            decimal price = 0;
            if (t.DistributeType == 1)
            {
                price = (t.WorkersNum * t.WorkLenth * gmWork.Money
                         + t.CheckNum * t.CheckLenth * gmTech.Money
                         + (equipment.ManageWork + equipment.SalesWork + equipment.ToolWork) * t.WorkLenth
                         + zhuMoney
                         + fuMoney) / ptv.Amount;
            }
            else
            {
                price = (zhuMoney + fuMoney + t.CheckNum * t.CheckLenth * gmTech.Money) / ptv.Amount;
            }


            ViewBag.Price = price;//单价
            ViewBag.Equipment = equipment;
            ViewBag.OrderId = ptv.OrderId;
            ViewBag.Technology = t;
            ViewBag.FinanceCost = financeCost;
            ViewBag.GradeMaintenanceTech = gmTech;
            ViewBag.GradeMaintenanceWork = gmWork;
            ViewBag.OperationName = operatorModel.FamilyName;//操作人
            ViewBag.OperationTime = DateTime.Now.ToString("yyyy-MM-dd");//操作时间
            return View(ptv);
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public string AddCheck(FinanceCost financeCost, Guid orderId)
        {

            FinanceCost f = _financeCostApp.GetSingleFinanceCostByOrderProductId(financeCost.OrderProductId);
            if (null != f)
            {
                DateTime nowTime = DateTime.Now;

                f.TestWork = financeCost.TestWork;
                f.ManageWork = financeCost.ManageWork;
                f.OperateWork = financeCost.OperateWork;
                f.ToolWork = financeCost.ToolWork;
                f.SalesWork = financeCost.SalesWork;
                f.CreateTime = nowTime;
                f.CreatorId = operatorModel.UserId;
                f.CreatorName = operatorModel.FamilyName;
                f.Status = 1;
                f.PriceConfirm = financeCost.PriceConfirm;
                _financeCostApp.Update(f);//修改财务审核内容

                //x修改关联表的价格
                OrderProduct op = _orderProductApp.GetSingleOrderProduct(f.OrderProductId);
                if (op != null)
                {
                    op.Price = financeCost.PriceConfirm;
                    op.CostPrice = financeCost.PriceConfirm;
                    op.Progress = 4;
                    _orderProductApp.Update(op);
                }
                if (financeCost.Status == -1)
                {
                    OperateRecord o = new OperateRecord();
                    o.ItemId = f.Id;//财务表id
                    o.TypeId = 7;
                    o.EndTime = nowTime;
                    o.CreatorId = operatorModel.UserId;
                    o.CreatorName = operatorModel.FamilyName;
                    _operateRecordApp.UpdateOperateRecord(o);//修改本操作人员工作记录日志

                    Order order = _operateRecordApp.GetSingleOrder(orderId);
                    if (order != null && order.Status == 4)
                    {
                        int noPass = _financeCostApp.GetNoPassFinanceCostByOrderId(orderId);
                        int count = _orderProductApp.GetCheckOrderProductByOrderId(orderId);
                        if (noPass == 0 && count == 0)
                        {
                            //_operateRecordApp.UpdateOrderStateAndIniById(orderId, 5, financeCost.PriceConfirm);//更改订单Status=5并且修改initPrice字段
                            _operateRecordApp.UpdateOrderStateById(orderId, 5);//更改订单Status=5
                        }
                    }

                    ProfitRatio fc = new ProfitRatio();//插入财务总监审核表
                    Guid prnewId = Guid.NewGuid();
                    fc.Id = prnewId;
                    fc.OrderId = orderId;
                    fc.OrderProductId = financeCost.OrderProductId;
                    fc.Status = -1;
                    fc.CreatorId = null;
                    fc.CreateTime = null;
                    fc.ApplyTime = nowTime;
                    fc.ApplyId = operatorModel.UserId;
                    _profitRatioApp.Add(fc);

                    //插入给下一个工作人员添加人工记录
                    OperateRecord o1 = new OperateRecord();
                    o1.Id = Guid.NewGuid();
                    o1.ItemId = prnewId;//总监id
                    o1.TypeId = 9;
                    o1.StartTime = nowTime;
                    o1.EndTime = null;
                    o1.CreatorId = null;
                    _operateRecordApp.AddOperateRecord(o1);
                }
            }
            return Result.ToJson();
        }

        public ActionResult QueryOrder(int page = 1, string productName = "", string number = "", string beginTime = "", string endTime = "")
        {
            ViewBag.productName = productName.Trim();
            ViewBag.number = number.Trim();
            DateTime _beginTime = DateTime.Parse("2000-01-01 10:10:10");
            DateTime _endTime = DateTime.Now.Date.AddDays(1);
            ViewBag.beginTime = "";
            ViewBag.endTime = "";
            if (!string.IsNullOrEmpty(beginTime) && Validate.IsDate(beginTime))
            {
                _beginTime = DateTime.Parse(beginTime.Trim());
                ViewBag.beginTime = beginTime.Trim();
            }
            if (!string.IsNullOrEmpty(endTime) && Validate.IsDate(endTime))
            {
                _endTime = DateTime.Parse(endTime.Trim());
                ViewBag.endTime = endTime.Trim();
            }

            IPagedList<ProductTechnologyView> tecjList = _financeCostApp.GetList(-10, 1, productName.Trim(), number.Trim(), _beginTime, _endTime).ToPagedList(page, 10);
            return View(tecjList);
        }

        public ActionResult QueryInfo(Guid opId)
        {
            //IEnumerable<ProductTechnologyView> list = _orderProductApp.GetOrderProductsByOrderId(tId);
            ProductTechnologyView ptv = _orderProductApp.GetOrderProductsByOrderProductId(opId);

            FinanceCost fc = _financeCostApp.GetSingleFinanceCostByOrderProductId(ptv.OrderProductId);
            ViewBag.FinanceCost = fc;
            ViewBag.OperationTime = fc.CreateTime.ToString("yyyy-MM-dd");//操作时间
            return View(ptv);
        }


        public ActionResult ReceivableQuery(int page = 1, string number = "", string takeCompany = "")
        {
            ViewBag.Number = number.Trim();
            ViewBag.TakeCompany = takeCompany.Trim();

            IPagedList<InvoiceView> tecjList = _financeInvoiceApp.GetFinanceInvoicesList(-1, number.Trim()).ToPagedList(page, 10);
            return View(tecjList);
        }

        public ActionResult FinanceInvoiceLayout(Guid fid)
        {
            FinanceInvoice financeInvoice = _financeInvoiceApp.GetSingleFinanceInvoice(fid);
            Order order = _orderApp.GetInfo(financeInvoice.OrderId);
            Customer customer = _customerApp.GetInfo(order.CustomeId.Value);

            ViewBag.Order = order;
            ViewBag.Customer = customer;
            ViewBag.OperationName = operatorModel.FamilyName;//操作人
            ViewBag.OperationTime = DateTime.Now.ToString("yyyy-MM-dd");//操作时间
            return View(financeInvoice);
        }

        public ActionResult YingshouQuery(int page = 1, string financeNumber = "", string customerName = "")
        {
            ViewBag.FinanceNumber = financeNumber.Trim();
            ViewBag.CustomerName = customerName.Trim();

            IPagedList<ArapView> arapList = _arapApp.GetListArapViews(1, -1, financeNumber.Trim(), customerName.Trim()).ToPagedList(page, 10);
            return View(arapList);
        }

        public ActionResult CompleteYingshou(int page = 1, string financeNumber = "", string customerName = "")
        {
            ViewBag.FinanceNumber = financeNumber.Trim();
            ViewBag.CustomerName = customerName.Trim();

            IPagedList<ArapView> arapList = _arapApp.GetListArapViews(1, 1, financeNumber.Trim(), customerName.Trim()).ToPagedList(page, 10);
            return View(arapList);
        }

        /// <summary>
        /// 修改票号
        /// </summary>
        /// <param name="arapId"></param>
        /// <param name="financeNumber"></param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        public string UpdateYingshouInfoNumber(Guid arapId, string financeNumber)
        {
            Arap arap = _arapApp.GetSingle(arapId);
            arap.FinanceNumber = financeNumber;
            arap.InvoiceType = 0;
            var financeInvoice = _financeInvoiceApp.GetSingleFinanceInvoice(arap.FinanceInvoiceId);
            financeInvoice.Number = financeNumber;
            financeInvoice.Type = 0;
            _arapApp.Update(arap);
            _financeInvoiceApp.Update(financeInvoice);
            return new { Code = 200 }.ToJson();
        }

        public ActionResult YingshouInfo(Guid id)
        {
            ViewBag.OperationName = operatorModel.FamilyName;//操作人
            ViewBag.OperationTime = DateTime.Now.ToString("yyyy-MM-dd");//操作时间
            Arap arap = _arapApp.GetSingle(id);
            FinanceInvoice financeInvoice = null;
            Order order = null;
            Customer customer = null;
            decimal amount = 0;

            if (arap != null)
            {
                financeInvoice = _financeInvoiceApp.GetSingleFinanceInvoice(arap.FinanceInvoiceId);
                order = _orderApp.GetInfo(arap.OrderId);
                if (order != null && order.CustomeId != null)
                {
                    customer = _customerApp.GetInfo(order.CustomeId.Value);
                    if (financeInvoice != null)
                    {
                        List<DeliveryProduct> itemList = Common.Json.ToObject<List<DeliveryProduct>>(financeInvoice.ProductData);
                        foreach (var item in itemList)
                        {
                            amount += item.TotalMoney;
                        }
                    }
                    if (arap.Status == 1)
                    {
                        ViewBag.OperationName = arap.CreatorName;//操作人
                        ViewBag.OperationTime = arap.CreateTime.Value.ToString("yyyy-MM-dd");//操作时间
                    }
                }
            }

            ViewBag.FinanceInvoice = financeInvoice;
            ViewBag.Customer = customer;
            ViewBag.Order = order;
            ViewBag.Amount = amount;

            return View(arap);
        }

        public ActionResult YingFuQuery(int page = 1, string customerName = "", string financeNumber = "")
        {
            ViewBag.CustomerName = customerName.Trim();
            ViewBag.FinanceNumber = financeNumber.Trim();

            IPagedList<ArapView> arapList = _arapApp.GetListArapYingfuViews(2, -10, customerName.Trim(), financeNumber.Trim()).ToPagedList(page, 10);
            return View(arapList);
        }

        public ActionResult YingFuInfo(string financeNumber)
        {
            ViewBag.OperationName = operatorModel.FamilyName;//操作人
            ViewBag.OperationTime = DateTime.Now.ToString("yyyy-MM-dd");//操作时间
            List<Arap> arapList = _arapApp.GetListArapByName(financeNumber);
            var objStatus = arapList.FirstOrDefault(a => a.Status == -1);
            if (objStatus == null)
            {
                ViewBag.yinfuSta = "1";
            }
            else
            {
                ViewBag.yinfuSta = "-1";
            }
            List<ArapView> list = new List<ArapView>();
            foreach (var item in arapList)
            {
                ArapView web = _arapApp.GetSingleArapView(item.Id);
                if (web != null)
                {
                    list.Add(web);
                }
            }
            ViewBag.FinanceNumber = financeNumber;
            return View(list.OrderBy(a => a.Status).ToList());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="web"></param>
        /// <param name="chousetype">1：应收  2：应付</param>
        /// <returns></returns>
        [HttpPost]
        [HandlerAjaxOnly]
        public string UpdateArap(Arap web, int chousetype)
        {
            DateTime nowTime = DateTime.Now;
            if (chousetype == 1)
            {
                decimal acm = 0;
                Arap arap = _arapApp.GetSingle(web.Id);
                arap.ShouldMoney = web.ShouldMoney;
                arap.Memo = web.Memo;
                //arap.Status = 1;
                arap.CreateTime = nowTime;
                arap.CreatorId = operatorModel.UserId;
                arap.CreatorName = operatorModel.FamilyName;


                FinanceInvoice financeInvoice = _financeInvoiceApp.GetSingleFinanceInvoice(arap.FinanceInvoiceId);
                //financeInvoice.Status = 3;
                if (!string.IsNullOrEmpty(web.Data))
                {
                    List<DeliveryProduct> listNew = Common.Json.ToObject<List<DeliveryProduct>>(web.Data);
                    List<DeliveryProduct> listOld = Common.Json.ToObject<List<DeliveryProduct>>(financeInvoice.ProductData);
                    foreach (var item in listNew)
                    {
                        var obj = listOld.FirstOrDefault(a => a.ProductId == item.ProductId);
                        if (obj != null)
                        {
                            obj.StrEndTime = item.StrEndTime;
                            if (string.IsNullOrEmpty(item.StrEndTime))
                            {
                                obj.StrEndTime = nowTime.ToString("yyyy-MM-dd");
                            }
                            obj.CumulativeMoney = obj.CumulativeMoney + item.ItemActualMoney;//累计单个产品金额
                            acm += obj.CumulativeMoney;//累计应付总金额
                        }
                    }
                    financeInvoice.ProductData = listOld.ToJson();
                }

                arap.ActualMoney = acm;
                if (acm >= arap.ShouldMoney)
                {
                    arap.Status = 1;
                }
                _arapApp.Update(arap);
                _financeInvoiceApp.Update(financeInvoice);
            }
            if (chousetype == 2)
            {
                if (!string.IsNullOrEmpty(web.Data))
                {
                    List<Arap> proList = Common.Json.ToObject<List<Arap>>(web.Data);
                    //proList = proList.Where(a => a.Status == -1).ToList();
                    foreach (var item in proList)
                    {
                        Arap arap = _arapApp.GetSingle(item.Id);
                        arap.ShouldMoney = item.ShouldMoney;
                        arap.ActualMoney = item.ActualMoney;
                        arap.TotalMoney = item.ActualMoney + arap.TotalMoney;
                        if (arap.ShouldMoney <= arap.TotalMoney)
                        {
                            arap.Status = 1;
                        }
                        /*else if (web.Status == -1)
                        {
                            arap.Status = -1;
                        }
                        //手动修改状态
                        if (web.Status == 1)
                        {
                            arap.Status = 1;
                        }*/

                        if (item.CreateTime == null)
                        {
                            arap.CreateTime = nowTime;
                        }
                        else
                        {
                            arap.CreateTime = item.CreateTime;
                        }
                        arap.CreatorId = operatorModel.UserId;
                        arap.CreatorName = operatorModel.FamilyName;
                        _arapApp.Update(arap);

                        if (arap.Status == 1)
                        {
                            OperateRecord o = new OperateRecord();
                            o.ItemId = arap.Id;
                            o.TypeId = 24;
                            o.EndTime = nowTime;
                            o.CreatorId = operatorModel.UserId;
                            o.CreatorName = operatorModel.FamilyName;
                            _operateRecordApp.UpdateOperateRecord(o);//修改本操作人员工作记录日志
                        }
                    }
                }
            }

            return Result.ToJson();
        }

        /*public ActionResult ReceivableInfo(Guid inId)
        {
            Invoice invoice = _invoiceApp.GetSingleInvoice(inId);
            Order order = _orderApp.GetInfo(invoice.OrderId);
            Customer customer = _customerApp.GetInfo(order.CustomeId.Value);
            InvoceType invoceType = _invoiceApp.GetSingleInvoceType(invoice.Type);
            SendCar sc = _invoiceApp.GetSingleSendCar(invoice.Id);
            ExpressOrder exp = _invoiceApp.GetSingleExpressOrder(invoice.Id);

            //对账完成
            InvoiceOther io = _invoiceApp.GetSingleInvoiceOther(invoice.Id, 1);
            ViewBag.InvoiceOther1 = io;

            //寄送完成
            InvoiceOther ioo = _invoiceApp.GetSingleInvoiceOther(invoice.Id, 2);
            ViewBag.InvoiceOther2 = ioo;

            CollectAndPayMent cap = _invoiceApp.GetSingleCollectAndPayMent(invoice.Id, invoice.OrderId);
            ViewBag.CollectAndPayMent = cap;

            ViewBag.InvoceTypeName = invoceType.TypeName;
            ViewBag.CustomerShortName = customer.ShortName;
            ViewBag.Invoice = invoice;
            ViewBag.SendCar = sc;
            ViewBag.ExpressOrder = exp;
            ViewBag.OperationName = operatorModel.FamilyName;//操作人
            ViewBag.OperationTime = DateTime.Now.ToString("yyyy-MM-dd");//操作时间
            return View(order);
        }*/

        [HttpPost]
        [HandlerAjaxOnly]
        public string AddCollectAndPayMent(CollectAndPayMent cap, int type)
        {
            DateTime nowTime = DateTime.Now;

            if (type == 5)
            {
                Invoice i = _invoiceApp.GetSingleInvoice(cap.InvoicId);
                i.Status = 5;
                _invoiceApp.Update(i);//修改发货单状态
            }
            cap.Id = Guid.NewGuid();
            cap.CreateTime = nowTime;
            cap.CreatorId = operatorModel.UserId;
            cap.CreatorName = operatorModel.FamilyName;
            cap.Type = 1;
            cap.Status = -1;
            _invoiceApp.AddCollectAndPayMent(cap);//插入应收账单

            return Result.ToJson();
        }

        public ActionResult FinanceInvoiceInfo(Guid ficeId)
        {
            //_financeInvoiceApp
            FinanceInvoice financeInvoice = _financeInvoiceApp.GetSingleFinanceInvoice(ficeId);
            Order order = _orderApp.GetInfo(financeInvoice.OrderId);
            Customer customer = _customerApp.GetInfo(order.CustomeId.Value);

            ViewBag.Order = order;
            ViewBag.Customer = customer;
            ViewBag.OperationName = operatorModel.FamilyName;//操作人
            ViewBag.OperationTime = DateTime.Now.ToString("yyyy-MM-dd");//操作时间
            return View(financeInvoice);
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public string UpdateFinanceInvoice(FinanceInvoice web, int type)
        {
            DateTime nowTime = DateTime.Now;
            FinanceInvoice financeInvoice = _financeInvoiceApp.GetSingleFinanceInvoice(web.Id);
            financeInvoice.Status = type;
            financeInvoice.Number = web.Number;
            financeInvoice.CreateTime = nowTime;
            financeInvoice.CreatorId = operatorModel.UserId;
            financeInvoice.CreatorName = operatorModel.FamilyName;
            financeInvoice.FinanceInvoiceMemo = web.FinanceInvoiceMemo;
            if (!string.IsNullOrEmpty(financeInvoice.ProductData))
            {
                List<DeliveryProduct> itemList = Common.Json.ToObject<List<DeliveryProduct>>(financeInvoice.ProductData);
                foreach (var item in itemList)
                {
                    item.StrCreateTime = nowTime.ToString("yyyy-MM-dd");
                    item.StrEndTime = "";
                }
                financeInvoice.ProductData = itemList.ToJson();
            }
            _financeInvoiceApp.Update(financeInvoice);

            if (type == 1)
            {
                OperateRecord o = new OperateRecord();
                o.ItemId = financeInvoice.Id;
                o.TypeId = 27;
                o.EndTime = nowTime;
                o.CreatorId = operatorModel.UserId;
                o.CreatorName = operatorModel.FamilyName;
                _operateRecordApp.UpdateOperateRecord(o);//修改本操作人员工作记录日志

                OperateRecord o1 = new OperateRecord();
                o1.Id = Guid.NewGuid();
                o1.ItemId = financeInvoice.Id;
                o1.TypeId = 28;
                o1.StartTime = nowTime;
                o1.EndTime = null;
                o1.CreatorId = null;
                _operateRecordApp.AddOperateRecord(o1);
            }


            //插入应收应付表
            if (type == 2)
            {
                Arap arap = new Arap();
                Guid newGuid = Guid.NewGuid();
                arap.Id = newGuid;
                arap.Status = -1;
                arap.FinanceInvoiceId = financeInvoice.Id;
                arap.OrderId = financeInvoice.OrderId;
                arap.ApplyTime = DateTime.Now;
                arap.FinanceNumber = financeInvoice.Number;
                arap.Type = 1;
                arap.InvoiceType = 0;
                arap.TotalMoney = web.TotalMoney;
                _arapApp.Add(arap);

                OperateRecord o = new OperateRecord();
                o.ItemId = financeInvoice.Id;
                o.TypeId = 28;
                o.EndTime = nowTime;
                o.CreatorId = operatorModel.UserId;
                o.CreatorName = operatorModel.FamilyName;
                _operateRecordApp.UpdateOperateRecord(o);//修改本操作人员工作记录日志

                OperateRecord o1 = new OperateRecord();
                o1.Id = Guid.NewGuid();
                o1.ItemId = newGuid;
                o1.TypeId = 25;
                o1.StartTime = nowTime;
                o1.EndTime = null;
                o1.CreatorId = null;
                _operateRecordApp.AddOperateRecord(o1);
            }
            return Result.ToJson();
        }



        public ActionResult TechType(int page = 1, string equipmentName = "")
        {
            ViewBag.EquipmentName = equipmentName.Trim();
            IPagedList<Equipment> eqList = _technologyApp.GetListEquipment(equipmentName.Trim()).ToPagedList(page, 10);
            return View(eqList);
        }

        public ActionResult TechTypeService(Guid eId)
        {
            ViewBag.Sign = "1";
            Equipment web = _technologyApp.GetSingleEquipment(eId);

            return View(web);
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public string AddOrUpdateEquipment(Equipment equipment, string sign)
        {

            //修改
            if (sign == "1")
            {
                _technologyApp.UpdateEquipment(equipment);
            }
            //添加
            if (sign == "-1")
            {
                _technologyApp.InsertEquipment(equipment);
            }

            return Result.ToJson();
        }

        public ActionResult GradeMaintenanceService()
        {
            List<GradeMaintenance> gmList = _technologyApp.GetListGradeMaintenance(1);
            return View(gmList);
        }


        [HttpPost]
        [HandlerAjaxOnly]
        public string AddOrUpdateGradeMaintenance(GradeMaintenance gGradeMaintenance, string sign)
        {
            if (gGradeMaintenance.Data != null)
            {
                List<GradeMaintenance> gmList = Common.Json.ToObject<List<GradeMaintenance>>(gGradeMaintenance.Data);
                //修改
                if (sign == "1")
                {
                    foreach (var item in gmList)
                    {
                        GradeMaintenance web = _technologyApp.GetSingleGradeMaintenance(item.Id);
                        web.Money = item.Money;
                        _technologyApp.UpdateGradeMaintenance(web);
                    }
                }
                //添加
                if (sign == "-1")
                {
                    foreach (var item in gmList)
                    {
                        item.Id = Guid.NewGuid();
                        _technologyApp.InsertGradeMaintenance(item);
                    }
                    //_technologyApp.InsertGradeMaintenanceBatch(gmList);
                }
            }
            return Result.ToJson();
        }
        public ActionResult GradeMaintenanceTwo()
        {
            List<GradeMaintenance> gmList = _technologyApp.GetListGradeMaintenance(2);
            return View(gmList);
        }
    }
}